﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;

namespace Runda.EShop.Common.Component
{
    [ToolboxData("<{0}:GridRadioButton runat=server></{0}:GridRadioButton>")]
    public class GridRadioButton : RadioButton, IPostBackDataHandler
    {

        public GridRadioButton()
            : base()
        {

        }

        #region Properties

        private string Value
        {
            get
            {
                string val = Attributes["value"];
                //if (val == null)
                //    val = UniqueID;
                //else
                //    val = UniqueID + "_" + val;
                return val;
            }
            set
            {
                Attributes["value"] = value;
            }
        }

        #endregion


        protected override void Render(HtmlTextWriter writer)
        {
            RenderInputTag(writer);
        }

        private void RenderInputTag(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Id, ClientID);
            writer.AddAttribute(HtmlTextWriterAttribute.Type, "radio");
            writer.AddAttribute(HtmlTextWriterAttribute.Name, GroupName);
            writer.AddAttribute(HtmlTextWriterAttribute.Value, Value);
            if (Checked)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Checked, "checked");
            }
            if (!Enabled)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Disabled, "disabled");
            }

            string onClick = Attributes["onclick"];

            if (AutoPostBack)
            {
                if (onClick != null)
                {
                    onClick = string.Empty;
                    onClick += Page.ClientScript.GetPostBackEventReference(this, string.Empty);
                    writer.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick);
                    writer.AddAttribute("language", "javascript");
                }
            }
            else
            {
                if (onClick != null)
                {
                    writer.AddAttribute(HtmlTextWriterAttribute.Onclick, onClick);
                }
            }

            if (AccessKey.Length > 0)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Accesskey, AccessKey);

            }
            if (TabIndex != 0)
            {
                writer.AddAttribute(HtmlTextWriterAttribute.Tabindex, TabIndex.ToString(NumberFormatInfo.InvariantInfo));
            }

            writer.RenderBeginTag(HtmlTextWriterTag.Input);
            writer.RenderEndTag();
        }


        protected override void RenderContents(HtmlTextWriter output)
        {
            output.Write(Text);
        }

        #region IPostBackDataHandler Members

        bool IPostBackDataHandler.LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
        {
            bool result = false;
            string value = postCollection[GroupName];
            if (value != null && value == Value)
            {
                if (!Checked)
                {
                    Checked = true;
                    result = true;
                }
            }
            else
            {
                if (Checked)
                {
                    Checked = false;
                }
            }
            return result;
        }

        void IPostBackDataHandler.RaisePostDataChangedEvent()
        {
            OnCheckedChanged(EventArgs.Empty);
        }

        #endregion
    }
}
